{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook, I will show how to train the TensorFlow version of Sketch-RNN on a new dataset, and convert the weights of the TF model to a JSON format that is usable by Sketch-RNN-JS so that interactive web demos can be built.\n",
    "\n",
    "For the purpose of this tutorial, I will be training on the dataset file called `kanji.rdp25.npz` which is available inside the repo `https://github.com/hardmaru/sketch-rnn-datasets/` under the `kanji` subdirectory. If you have a custom dataset, you will need to convert it over to an .npz file using the stroke-3 format as done for these datasets. Please study the README.md in Sketch-RNN to understand how the file format that Sketch-RNN can work with work, in the section called [\"Creating Your Own Dataset\"](https://github.com/tensorflow/magenta/blob/master/magenta/models/sketch_rnn/README.md).\n",
    "\n",
    "After cloning the TensorFlow repo for the Sketch-RNN [model](https://github.com/tensorflow/magenta/tree/master/magenta/models/sketch_rnn), below is the command that I ran to train the TensorFlow model:\n",
    "\n",
    "```\n",
    "python sketch_rnn_train.py --data_dir=kanji --hparams=data_set=['kanji.rdp25.npz'],num_steps=200000,conditional=0,dec_rnn_size=1024\n",
    "```\n",
    "\n",
    "I store the `kanji.rdp25.npz` inside the subdirectory called `kanji` but you can use whatever you want. The important thing to note here is that I'm trainining a decoder-only model by setting `conditional=0` and I'm training a 1 layer LSTM with hidden size of 1024, which should be good enough for most datasets in the order of 10K size. Using 200K steps should take around half a day on a single P100 GPU, so it should cost around USD 10 dollars using the current prices for Google Cloud Platform to train this model.\n",
    "\n",
    "After the model is trained, I run the remaining commands for this IPython notebook to generate a file call `custom.gen.json`, which can be used in the Sketch-RNN-JS repo for interactive work:\n",
    "\n",
    "https://github.com/tensorflow/magenta-demos/tree/master/sketch-rnn-js\n",
    "\n",
    "This `json` format created will also work for future TensorFlow.js and ML5.js versions of sketch-RNN."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# import the required libraries\n",
    "import numpy as np\n",
    "import time\n",
    "import random\n",
    "\n",
    "import codecs\n",
    "import collections\n",
    "import os\n",
    "import math\n",
    "import json\n",
    "import tensorflow as tf\n",
    "from six.moves import xrange\n",
    "\n",
    "# libraries required for visualisation:\n",
    "from IPython.display import SVG, display\n",
    "import svgwrite # conda install -c omnia svgwrite=1.1.6\n",
    "import PIL\n",
    "from PIL import Image\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# set numpy output to something sensible\n",
    "np.set_printoptions(precision=8, edgeitems=6, linewidth=200, suppress=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:TensorFlow Version: 1.8.0\n"
     ]
    }
   ],
   "source": [
    "tf.logging.info(\"TensorFlow Version: %s\", tf.__version__)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# import our command line tools\n",
    "'''\n",
    "from magenta.models.sketch_rnn.sketch_rnn_train import *\n",
    "from magenta.models.sketch_rnn.model import *\n",
    "from magenta.models.sketch_rnn.utils import *\n",
    "from magenta.models.sketch_rnn.rnn import *\n",
    "'''\n",
    "\n",
    "# If code is modified to remove magenta dependencies:\n",
    "from sketch_rnn_train import *\n",
    "from model import *\n",
    "from utils import *\n",
    "from rnn import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# little function that displays vector images and saves them to .svg\n",
    "def draw_strokes(data, factor=0.2, svg_filename = '/tmp/sketch_rnn/svg/sample.svg'):\n",
    "  tf.gfile.MakeDirs(os.path.dirname(svg_filename))\n",
    "  min_x, max_x, min_y, max_y = get_bounds(data, factor)\n",
    "  dims = (50 + max_x - min_x, 50 + max_y - min_y)\n",
    "  dwg = svgwrite.Drawing(svg_filename, size=dims)\n",
    "  dwg.add(dwg.rect(insert=(0, 0), size=dims,fill='white'))\n",
    "  lift_pen = 1\n",
    "  abs_x = 25 - min_x \n",
    "  abs_y = 25 - min_y\n",
    "  p = \"M%s,%s \" % (abs_x, abs_y)\n",
    "  command = \"m\"\n",
    "  for i in xrange(len(data)):\n",
    "    if (lift_pen == 1):\n",
    "      command = \"m\"\n",
    "    elif (command != \"l\"):\n",
    "      command = \"l\"\n",
    "    else:\n",
    "      command = \"\"\n",
    "    x = float(data[i,0])/factor\n",
    "    y = float(data[i,1])/factor\n",
    "    lift_pen = data[i, 2]\n",
    "    p += command+str(x)+\",\"+str(y)+\" \"\n",
    "  the_color = \"black\"\n",
    "  stroke_width = 1\n",
    "  dwg.add(dwg.path(p).stroke(the_color,stroke_width).fill(\"none\"))\n",
    "  dwg.save()\n",
    "  display(SVG(dwg.tostring()))\n",
    "\n",
    "# generate a 2D grid of many vector drawings\n",
    "def make_grid_svg(s_list, grid_space=10.0, grid_space_x=16.0):\n",
    "  def get_start_and_end(x):\n",
    "    x = np.array(x)\n",
    "    x = x[:, 0:2]\n",
    "    x_start = x[0]\n",
    "    x_end = x.sum(axis=0)\n",
    "    x = x.cumsum(axis=0)\n",
    "    x_max = x.max(axis=0)\n",
    "    x_min = x.min(axis=0)\n",
    "    center_loc = (x_max+x_min)*0.5\n",
    "    return x_start-center_loc, x_end\n",
    "  x_pos = 0.0\n",
    "  y_pos = 0.0\n",
    "  result = [[x_pos, y_pos, 1]]\n",
    "  for sample in s_list:\n",
    "    s = sample[0]\n",
    "    grid_loc = sample[1]\n",
    "    grid_y = grid_loc[0]*grid_space+grid_space*0.5\n",
    "    grid_x = grid_loc[1]*grid_space_x+grid_space_x*0.5\n",
    "    start_loc, delta_pos = get_start_and_end(s)\n",
    "\n",
    "    loc_x = start_loc[0]\n",
    "    loc_y = start_loc[1]\n",
    "    new_x_pos = grid_x+loc_x\n",
    "    new_y_pos = grid_y+loc_y\n",
    "    result.append([new_x_pos-x_pos, new_y_pos-y_pos, 0])\n",
    "\n",
    "    result += s.tolist()\n",
    "    result[-1][2] = 1\n",
    "    x_pos = new_x_pos+delta_pos[0]\n",
    "    y_pos = new_y_pos+delta_pos[1]\n",
    "  return np.array(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "define the path of the model you want to load, and also the path of the dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# you may need to change these to link to where your data and checkpoints are actually stored!\n",
    "# in the default config, model_dir is likely to be /tmp/sketch_rnn/models\n",
    "data_dir = './kanji'\n",
    "model_dir = './log'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Loaded 10358/600/500 from kanji.rdp25.npz\n",
      "INFO:tensorflow:Dataset combined: 11458 (10358/600/500), avg len 63\n",
      "INFO:tensorflow:model_params.max_seq_len 133.\n",
      "total images <= max_seq_len is 10358\n",
      "total images <= max_seq_len is 600\n",
      "total images <= max_seq_len is 500\n",
      "INFO:tensorflow:normalizing_scale_factor 14.4871.\n"
     ]
    }
   ],
   "source": [
    "[train_set, valid_set, test_set, hps_model, eval_hps_model, sample_hps_model] = load_env(data_dir, model_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "[hps_model, eval_hps_model, sample_hps_model] = load_model(model_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Model using gpu.\n",
      "INFO:tensorflow:Input dropout mode = False.\n",
      "INFO:tensorflow:Output dropout mode = False.\n",
      "INFO:tensorflow:Recurrent dropout mode = True.\n",
      "WARNING:tensorflow:From /Users/hadavid/devel/test/sketch_rnn/model.py:287: calling reduce_sum (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "keep_dims is deprecated, use keepdims instead\n",
      "WARNING:tensorflow:From /Users/hadavid/devel/test/sketch_rnn/model.py:297: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "\n",
      "Future major versions of TensorFlow will allow gradients to flow\n",
      "into the labels input on backprop by default.\n",
      "\n",
      "See @{tf.nn.softmax_cross_entropy_with_logits_v2}.\n",
      "\n",
      "INFO:tensorflow:Model using gpu.\n",
      "INFO:tensorflow:Input dropout mode = 0.\n",
      "INFO:tensorflow:Output dropout mode = 0.\n",
      "INFO:tensorflow:Recurrent dropout mode = 0.\n",
      "INFO:tensorflow:Model using gpu.\n",
      "INFO:tensorflow:Input dropout mode = 0.\n",
      "INFO:tensorflow:Output dropout mode = 0.\n",
      "INFO:tensorflow:Recurrent dropout mode = 0.\n"
     ]
    }
   ],
   "source": [
    "# construct the sketch-rnn model here:\n",
    "reset_graph()\n",
    "model = Model(hps_model)\n",
    "eval_model = Model(eval_hps_model, reuse=True)\n",
    "sample_model = Model(sample_hps_model, reuse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sess = tf.InteractiveSession()\n",
    "sess.run(tf.global_variables_initializer())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def decode(z_input=None, draw_mode=True, temperature=0.1, factor=0.2):\n",
    "  z = None\n",
    "  if z_input is not None:\n",
    "    z = [z_input]\n",
    "  sample_strokes, m = sample(sess, sample_model, seq_len=eval_model.hps.max_seq_len, temperature=temperature, z=z)\n",
    "  strokes = to_normal_strokes(sample_strokes)\n",
    "  if draw_mode:\n",
    "    draw_strokes(strokes, factor)\n",
    "  return strokes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Loading model ./log/vector-199000.\n",
      "INFO:tensorflow:Restoring parameters from ./log/vector-199000\n"
     ]
    }
   ],
   "source": [
    "# loads the weights from checkpoint into our model\n",
    "load_checkpoint(sess, model_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# randomly unconditionally generate 10 examples\n",
    "N = 10\n",
    "reconstructions = []\n",
    "for i in range(N):\n",
    "  reconstructions.append([decode(temperature=0.5, draw_mode=False), [0, i]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's see if our model kind of works by sampling from it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg baseProfile=\"full\" height=\"94.98375825191033\" version=\"1.1\" width=\"824.1533672867808\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:ev=\"http://www.w3.org/2001/xml-events\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs/><rect fill=\"white\" height=\"94.98375825191033\" width=\"824.1533672867808\" x=\"0\" y=\"0\"/><path d=\"M25,25 m0.0,0.0 m40.7488290569745,8.045297974022105 l0.07779640145599842,0.28362106531858444 -0.03662521718069911,1.6045601665973663 l-0.4155717045068741,1.9009970128536224 m-7.976159453392029,1.0431177914142609 l-0.13906167820096016,2.9652827978134155 0.44046543538570404,9.098562598228455 m-0.18711261451244354,-10.588773488998413 l13.138445615768433,-1.6668440401554108 1.111130565404892,0.21423108875751495 l0.3397144749760628,0.6083490327000618 -0.4651828110218048,3.5389891266822815 l-0.7094699889421463,3.455130457878113 m-6.117804050445557,-7.441446781158447 l0.2589019946753979,0.6197190657258034 0.10852918028831482,0.7619926333427429 m-5.977910161018372,1.1580049246549606 l9.279970526695251,-1.0590014606714249 3.393549919128418,-0.29952114447951317 m-11.546084880828857,3.944982886314392 l11.418637037277222,-1.0190476477146149 1.1513977497816086,0.0845407135784626 m-12.116719484329224,3.971424400806427 l0.14966164715588093,0.5629671365022659 0.3516774997115135,7.472051978111267 m-0.06603108253329992,-7.131349444389343 l9.952353239059448,-1.213163211941719 0.6349623948335648,0.11029963381588459 l0.2618616260588169,0.22749138996005058 0.07420187816023827,0.34962248057127 l-0.28596458956599236,2.917017638683319 -0.404108427464962,2.470300793647766 m-4.778777062892914,-5.938522219657898 l0.14634092338383198,0.5627260357141495 -0.07237030193209648,5.0751858949661255 m-5.068941712379456,-2.710280120372772 l7.5881510972976685,-0.6686561554670334 m-7.6726460456848145,3.7925463914871216 l9.34436559677124,-0.7010155916213989 m-12.137176990509033,4.398017525672913 l1.362219899892807,0.13289588503539562 8.135536909103394,-1.3325004279613495 l1.774003505706787,-0.024458339903503656 0.7612376660108566,0.14861198142170906 m-9.403250217437744,2.1154870092868805 l0.1904718019068241,0.5277367681264877 0.07266308646649122,0.6864219903945923 l-0.5652615800499916,4.762154817581177 0.061579691246151924,0.4139973595738411 l0.24123843759298325,0.05235372111201286 4.875156283378601,-1.8364913761615753 m1.1908771097660065,-4.329094588756561 l0.10950414463877678,0.49352411180734634 -0.08575666695833206,0.5886010080575943 l-1.4942777156829834,2.582017183303833 m-2.783242166042328,-3.40249240398407 l0.26516351848840714,0.5142667517066002 0.2915145084261894,0.8911780267953873 l0.23302171379327774,3.6803999543190002 0.12709402479231358,1.8967381119728088 l0.1326426211744547,1.3724201917648315 0.31312357634305954,0.7084259390830994 l0.7075069099664688,0.518534705042839 1.0178091377019882,0.2781853824853897 l2.7009952068328857,-0.00560734246391803 1.1707381159067154,-0.24235650897026062 l0.4251028969883919,-0.2815905585885048 0.2526401914656162,-0.4109148681163788 l0.12332414276897907,-0.8059951663017273 m63.023760814685374,-29.562084639328532 l0.24586720392107964,0.07044015917927027 1.9567076861858368,1.0457440465688705 l0.9598592668771744,0.7231719791889191 0.5840399488806725,0.6193863227963448 m-6.022050976753235,5.131070613861084 l1.235448643565178,0.5266815796494484 1.2968271970748901,0.7793684303760529 l0.9733062982559204,0.7559528946876526 0.4255663976073265,0.5108330026268959 m-3.8452723622322083,11.683449745178223 l0.8435729146003723,0.05170529242604971 0.6075550615787506,-0.3750468045473099 l1.5714804828166962,-2.4313102662563324 1.6050389409065247,-2.9263433814048767 m7.593032121658325,-16.5554141998291 l0.6793795526027679,0.9510955214500427 0.7207220047712326,1.3814380764961243 l0.5836857110261917,1.5482544898986816 0.2685425244271755,1.1777910590171814 m7.780324220657349,-5.457937121391296 l0.021616811864078045,0.7118949294090271 -0.3959464654326439,1.1367027461528778 l-1.740691065788269,2.873343825340271 -1.7429259419441223,2.4185147881507874 m-10.105223655700684,1.5077009797096252 l1.5077322721481323,0.16335446387529373 14.073874950408936,-1.1680824309587479 l1.9330935180187225,-0.023875569459050894 1.1034216731786728,0.151138911023736 m-16.057612895965576,4.77751225233078 l0.15679644420742989,0.8958793431520462 -0.8212918043136597,3.0324915051460266 l-0.9860735386610031,2.5420409440994263 -1.2078651785850525,2.2832894325256348 l-0.10448431596159935,0.3673107549548149 m5.688813328742981,-6.930941939353943 l0.7078631967306137,1.0867000371217728 0.5921854078769684,1.5352827310562134 l0.4688071459531784,1.8138143420219421 0.23109523579478264,1.5932275354862213 m4.007554054260254,-8.395156860351562 l0.14695322141051292,0.8258707076311111 -0.3467375785112381,2.536582350730896 l-0.9022334218025208,4.010051488876343 -0.9646393358707428,2.6647019386291504 l-0.7875485718250275,1.4802002906799316 -1.8993432819843292,2.3960958421230316 l-1.6028603911399841,1.43077552318573 -1.5815162658691406,1.01272813975811 l-2.2195997834205627,1.1315204203128815 m2.5688979029655457,-12.187899351119995 l1.0193116962909698,0.21951699629426003 0.8380920439958572,0.04723362158983946 l7.755972146987915,-1.2667927145957947 0.3831707313656807,0.20808815956115723 l0.16344944015145302,0.2457631565630436 0.019179064547643065,0.34021172672510147 l-0.6027733907103539,2.294272780418396 -0.7865173369646072,2.6917490363121033 l-0.10575572960078716,0.7110439985990524 0.0388530851341784,0.5811046808958054 l0.057784849777817726,0.5811106786131859 0.15868362039327621,0.4439113289117813 l0.19487978890538216,0.2675073966383934 0.20390290766954422,0.11589067988097668 l0.1964850164949894,-0.02524693263694644 0.20816516131162643,-0.18487801775336266 l0.16134334728121758,-0.3894638270139694 0.09433050639927387,-0.6392152607440948 l0.06719296332448721,-0.9882085770368576 m-11.369967460632324,5.600817799568176 l0.7701438665390015,0.41538361459970474 0.7196348905563354,0.10252508334815502 l9.26556408405304,-0.9856264293193817 1.483607292175293,0.06923544686287642 l0.6819736957550049,0.18251938745379448 m70.16416144208051,-30.89607349713333 l0.21396713331341743,0.15203007496893406 1.20028555393219,1.4220738410949707 l0.6640821695327759,1.4206203818321228 m-9.03814971446991,2.94430673122406 l1.3129612803459167,0.19578490406274796 13.309065103530884,-1.346995085477829 l1.5858227014541626,0.08909028023481369 m-13.711211681365967,3.002954423427582 l1.1246714740991592,2.1643632650375366 0.6606850028038025,2.2945478558540344 m8.26514482498169,-6.173635721206665 l0.06842135917395353,1.0581254959106445 -2.196783423423767,4.663160741329193 m-13.134783506393433,2.343309670686722 l1.3858620822429657,0.12332559563219547 13.905954360961914,-1.5021990239620209 l4.347178936004639,-0.21673418581485748 1.8027472496032715,0.12753811664879322 m-18.48191499710083,5.343056917190552 l0.4221716895699501,0.8318754285573959 0.2801714465022087,1.2002381682395935 l1.1530746519565582,8.471618890762329 m-1.079956591129303,-10.04811406135559 l12.508819103240967,-1.5281309187412262 0.963052287697792,0.06418296601623297 l0.5130784958600998,0.5631883442401886 0.0869712233543396,0.9004173427820206 l-1.4499759674072266,8.859613537788391 m-11.972073316574097,-4.400039315223694 l9.785822629928589,-1.046159788966179 m-9.237761497497559,5.7408589124679565 l11.134399175643921,-1.1264602839946747 m-18.95173668861389,8.29101026058197 l2.042872905731201,0.16269464045763016 15.183967351913452,-1.46439328789711 l4.988009035587311,-0.2704848349094391 1.701674610376358,0.12501574121415615 m53.41977677802788,-23.030118095484795 l0.20530525594949722,0.0659294705837965 1.2831413745880127,0.06539913360029459 l8.226594924926758,-0.9212476760149002 0.7732738554477692,0.012052241945639253 m-4.815306663513184,-7.063875794410706 l0.40981967002153397,0.5373257398605347 0.21046139299869537,0.8300504833459854 l-0.07388485595583916,25.566983222961426 m-0.14112509787082672,-18.7725830078125 l-1.3950714468955994,3.7540578842163086 -1.2085448950529099,2.696583867073059 l-1.3913637399673462,2.5812721252441406 -2.0712754130363464,3.310682475566864 m7.139216661453247,-9.6026211977005 l1.3392606377601624,1.220758780837059 1.875491738319397,2.3483143746852875 m8.065641522407532,-14.681694507598877 l0.4722994193434715,0.3154938668012619 0.31318165361881256,0.5753176286816597 l-0.005599754513241351,3.683583438396454 m-7.198989391326904,0.36476947367191315 l1.5339450538158417,0.16395721584558487 12.446565628051758,-0.9803642332553864 l1.2054310739040375,0.08526709862053394 m-11.82790994644165,2.1171262860298157 l1.2261039018630981,1.8773387372493744 0.5340277031064034,1.5915614366531372 m6.740671396255493,-4.614127278327942 l0.10765265673398972,0.5134285241365433 -0.19225073978304863,0.6266738474369049 l-2.0009808242321014,3.6070048809051514 m-10.58253526687622,1.1962851881980896 l1.4665231108665466,0.12927266769111156 12.589608430862427,-1.1023033410310745 l2.914086878299713,-0.1311281882226467 1.4504429697990417,0.10354680940508842 m-16.25044345855713,4.651171565055847 l0.34864284098148346,0.5981073528528214 0.29882172122597694,1.3122321665287018 l0.8620087057352066,6.608491539955139 m-0.8984898775815964,-8.193156719207764 l11.857554912567139,-1.120988130569458 0.5976194515824318,0.10411331430077553 l0.3957755118608475,0.4567425698041916 0.09788466617465019,0.325872041285038 l-1.4240248501300812,7.385895252227783 m-11.014610528945923,-3.6704176664352417 l8.714747428894043,-0.7454752177000046 m-8.108993172645569,4.455300569534302 l10.193514823913574,-0.8234845846891403 m-11.813079118728638,4.4807252287864685 l1.7575368285179138,0.08423270657658577 9.326931238174438,-0.7622983306646347 l1.6155700385570526,-0.004762481839861721 m-14.312927722930908,4.952960908412933 l1.8304252624511719,0.1055300422012806 10.117038488388062,-0.6136135011911392 l3.6442983150482178,0.006570871919393539 m-8.816584348678589,-14.971206188201904 l0.3681863099336624,0.43273337185382843 0.1470396388322115,0.5244146659970284 l0.015485483454540372,13.361432552337646 m76.08657259639585,-27.749789851513924 l0.19464759156107903,0.14191941358149052 0.9569169580936432,0.9880085289478302 l0.8141304552555084,1.3845673203468323 m-10.285104513168335,1.893918216228485 l1.2570610642433167,0.15367161482572556 17.050647735595703,-1.2132781744003296 l0.7777578383684158,0.029365892987698317 m-16.757735013961792,1.6218893229961395 l-0.16037741675972939,6.7689865827560425 -0.31502749770879745,3.396351635456085 l-0.4996982589364052,2.9531803727149963 -0.5318021029233932,1.9965487718582153 l-0.6826145946979523,1.7980234324932098 -1.0463348776102066,2.0057696104049683 l-1.2800255417823792,1.9026216864585876 m-0.363999642431736,-18.03653359413147 l0.7467532902956009,0.607311986386776 0.9089438617229462,1.0366292297840118 l0.7426536828279495,1.1595995724201202 0.3201732784509659,0.7985138893127441 m-3.5876470804214478,5.696534514427185 l0.28436295688152313,0.11967415921390057 0.4219602793455124,-0.06682527251541615 l3.8562822341918945,-2.908239960670471 m7.899760007858276,-8.873007893562317 l0.053170095197856426,0.4339377209544182 -0.1361342240124941,0.5415767803788185 l-0.8278413116931915,1.4811889827251434 m-3.4265565872192383,0.5579152703285217 l0.3524298220872879,0.7183212786912918 0.7013287395238876,4.950943291187286 m-0.5147112905979156,-5.236022472381592 l6.414934992790222,-0.8050240576267242 0.36627117544412613,0.31779978424310684 l0.06520076654851437,0.5421629175543785 -0.7966453582048416,4.456371068954468 m-6.22261643409729,-2.2300300002098083 l4.726476669311523,-0.5174609273672104 m-4.254336655139923,2.7168139815330505 l5.205916166305542,-0.5193410068750381 m-4.630855619907379,1.6410467028617859 l0.32028354704380035,0.6232404708862305 -0.21088887006044388,1.693466454744339 l-0.48316270112991333,2.536700665950775 -0.6787163019180298,2.062128633260727 m1.5445715188980103,-5.309650301933289 l0.08638032712042332,0.6357762962579727 0.017582322470843792,0.7987221330404282 l-0.3633718937635422,2.0428043603897095 -0.36481089890003204,1.4479808509349823 l-0.3068462572991848,1.0402856767177582 -0.29391124844551086,0.8003370463848114 l-0.3004298359155655,0.5936822667717934 -0.2735166624188423,0.3897271677851677 l-0.2895793318748474,0.2358093485236168 -0.3361723944544792,0.14244779013097286 m7.224234938621521,-15.807924270629883 l0.15115358866751194,0.7677043229341507 -0.24327727034687996,2.7070456743240356 l-0.566704198718071,2.0541472733020782 -0.7001219689846039,1.4293190836906433 m1.6569633781909943,-6.214092373847961 l0.9474477916955948,0.008924254216253757 2.8283515572547913,-0.516744889318943 l0.3618483990430832,0.07228126283735037 0.16393400728702545,0.2199363335967064 l-0.5047841742634773,3.3552223443984985 -0.11596046388149261,2.0906932651996613 l0.21697085350751877,0.9305211901664734 0.45006725937128067,0.45846711844205856 l0.6535536050796509,0.15160647220909595 0.9386371076107025,-0.09916815906763077 l0.5883839726448059,-0.33724136650562286 0.3265830874443054,-0.9410850703716278 l0.21568140015006065,-1.5427589416503906 m-8.037482500076294,5.496522784233093 l0.5393996089696884,0.15016287565231323 5.1582688093185425,-0.645085796713829 l0.29500337317585945,0.07012966088950634 0.23076804354786873,0.22531108930706978 l0.04636792000383139,0.28745226562023163 -0.9148069471120834,1.8380668759346008 l-1.3651318848133087,2.128339260816574 -1.39669269323349,1.6725502908229828 l-1.5408557653427124,1.4914274215698242 -2.7864930033683777,2.0648911595344543 m1.8393084406852722,-7.821318507194519 l1.0568856447935104,0.8436565101146698 5.004401206970215,5.09113073348999 l1.3925184309482574,1.2138774245977402 0.8549300581216812,0.47096189111471176 m58.699045808753,-24.992640948621556 l0.11429397389292717,0.24804402142763138 0.24841681122779846,0.9580208361148834 l-0.011783658992499113,6.31513774394989 -0.156329907476902,3.5827329754829407 l-0.3220754861831665,3.2054531574249268 -1.1268328130245209,4.788035154342651 l-0.8313658833503723,1.943458467721939 -1.1846328526735306,1.980753242969513 m3.789514899253845,-22.58981227874756 l4.59211528301239,-0.7165755331516266 0.7382943481206894,0.11932970955967903 l0.315721333026886,0.4052504524588585 -0.17241163179278374,22.161290645599365 l-0.24815715849399567,0.6699354201555252 -0.3520086780190468,0.2510521747171879 l-0.38165315985679626,-0.0443248450756073 -0.81705242395401,-0.5010167509317398 m-3.2769960165023804,-15.748212337493896 l3.812706172466278,-0.3859507292509079 m-4.069879353046417,6.078729033470154 l3.9363738894462585,-0.32169222831726074 m8.045454025268555,-12.675670385360718 l0.023425815161317587,0.6458956748247147 -0.23490753024816513,0.7685878127813339 l-1.3636130094528198,2.621711790561676 -1.6068615019321442,2.261997312307358 l-1.902669072151184,2.042410373687744 m4.971534609794617,-4.747785031795502 l0.6981614977121353,0.14242365024983883 0.8086322993040085,-0.032547786831855774 l7.889966368675232,-1.0779641568660736 0.6889688968658447,0.13237377628684044 l0.44393520802259445,0.4460863023996353 0.0780663127079606,0.7640978693962097 l-0.4257586598396301,4.463755488395691 -0.695301815867424,4.56553041934967 l-0.9000881016254425,4.127043783664703 -0.7425572723150253,2.6709195971488953 l-0.5387474223971367,1.2317439913749695 -0.5300263315439224,0.4853389039635658 l-0.7158936560153961,0.08838910609483719 -0.9759766608476639,-0.8913314342498779 l-0.5155299976468086,-0.8400755375623703 m-7.043353319168091,-11.494770050048828 l0.2987666614353657,0.4925718158483505 0.16206324100494385,0.7328919321298599 m0.6251303851604462,-1.8685513734817505 l4.016372561454773,-0.6777138262987137 0.9507880359888077,0.34832801669836044 l0.1710178330540657,0.32588042318820953 -0.5696951225399971,2.97082781791687 m-5.251451730728149,0.8673765510320663 l5.880875587463379,-0.7136138528585434 0.7435932755470276,-0.03562497207894921 m-6.0087209939956665,-3.1444725394248962 l0.24764982983469963,0.42861126363277435 0.05397424567490816,0.4494619369506836 l0.010742141166701913,8.828628659248352 m-4.67899888753891,0.5919957906007767 l0.6021233648061752,0.1619640365242958 0.6541292369365692,0.020568182226270437 l7.794187664985657,-1.6224071383476257 m-1.5968628227710724,-2.929193675518036 l0.4479477182030678,0.7391369342803955 0.3044489398598671,0.9940961748361588 l0.12492243200540543,3.0784547328948975 m75.94462536741048,-23.8620541698765 l0.21222300827503204,0.17434727400541306 1.0856609791517258,1.1922833323478699 l0.8237292617559433,1.3447971642017365 m-10.046714544296265,2.7421963214874268 l1.4449457824230194,0.176776722073555 15.132983922958374,-1.1727486550807953 l1.271815150976181,0.055914283730089664 0.8756984025239944,0.21339545026421547 m-17.96102285385132,1.9010736048221588 l-0.17527246847748756,8.286102414131165 -0.32857004553079605,4.012431800365448 l-0.4023916646838188,2.7809906005859375 -0.485837385058403,2.022527754306793 l-0.7620139420032501,2.1787139773368835 -1.1841809004545212,2.489008903503418 l-1.377614289522171,2.2532930970191956 m-0.0307019567117095,-20.832021236419678 l0.7878521084785461,0.7498820871114731 0.9921271353960037,1.2267691642045975 l0.6663542240858078,1.0746726393699646 m-3.2390066981315613,7.268005609512329 l0.2838633209466934,0.12109445407986641 0.43499942868947983,-0.07263971026986837 l3.520050346851349,-3.2596972584724426 m8.80309283733368,-9.756962656974792 l0.6841240078210831,0.9587531536817551 0.5440356582403183,1.313316524028778 l0.42459718883037567,1.4723028242588043 m3.96576851606369,-3.947734832763672 l0.08465559221804142,0.6842046231031418 -0.19230039790272713,1.073692962527275 l-0.36300763487815857,1.0247381776571274 m-8.514968752861023,1.8967324495315552 l2.039833962917328,0.03822989063337445 8.962965607643127,-1.0292413830757141 l2.0113199949264526,-0.04260628949850798 m-12.649456262588501,4.790077805519104 l1.7006781697273254,0.07328120525926352 8.830872178077698,-0.9503427147865295 l1.0903679579496384,0.031080162152647972 m-13.409818410873413,4.445152580738068 l2.142424136400223,0.09745324961841106 9.956966042518616,-1.093333289027214 l2.505880296230316,-0.16090922057628632 m-9.09109890460968,-5.484917759895325 l0.2969446964561939,0.4431202635169029 0.13727381825447083,0.6485337764024734 l-0.00831934914458543,5.766390562057495 m-3.8769114017486572,1.811806559562683 l-0.02606461988762021,0.7466752082109451 -0.3933791071176529,0.8796148002147675 l-1.7000660300254822,2.5542479753494263 -2.108198255300522,2.3761093616485596 l-2.350543886423111,2.003293037414551 m8.799973130226135,-7.256835103034973 l1.2796920537948608,0.06677691824734211 6.0298967361450195,-0.8835309743881226 l0.9215406328439713,0.08077071979641914 m-5.933242440223694,1.3894207775592804 l0.22207573056221008,0.3287314996123314 0.0406369986012578,0.47415338456630707 m-4.835850298404694,1.5366870164871216 l1.52743399143219,0.07409898564219475 6.8019139766693115,-0.7772097736597061 l0.9797288477420807,0.07200172636657953 m-7.869697213172913,2.2736291587352753 l0.2258407324552536,0.7210532575845718 -0.026306298095732927,6.142973899841309 m-0.4567059502005577,-5.946500897407532 l7.984524965286255,-0.973052978515625 0.7516075670719147,0.12393231503665447 l0.23533597588539124,0.3018405847251415 0.0794664304703474,0.6088432297110558 l-0.11985206045210361,6.217975616455078 m-8.257054686546326,-4.293588697910309 l8.7704336643219,-0.7357305288314819 m-8.571853041648865,3.185994029045105 l8.560652732849121,-0.7281620055437088 m70.7797164391377,-34.34549665034865 l0.15712250024080276,0.23679211735725403 0.4894626885652542,0.9130996465682983 l0.0027876338572241366,3.5182449221611023 m-10.254825353622437,1.0592682659626007 l1.9631332159042358,0.17250992357730865 16.380226612091064,-1.583128422498703 l1.3628147542476654,-0.03438604064285755 1.1799273639917374,0.17784344032406807 m-19.441884756088257,2.638624906539917 l0.25558771565556526,0.5231175571680069 0.07747524417936802,0.6440374255180359 l-0.23132465779781342,3.888838291168213 -0.4613383486866951,4.140388667583466 l-1.4055578410625458,6.689348816871643 -0.8592227846384048,2.7820566296577454 l-0.904010534286499,2.2080306708812714 -1.0449087619781494,1.9501005113124847 m8.148577809333801,-20.058128833770752 l1.4475037157535553,0.09761307388544083 8.928141593933105,-1.2843626737594604 l1.5927137434482574,-0.0025352402008138597 0.6249838694930077,0.19060064107179642 l0.39536111056804657,0.5546566471457481 -0.9277106076478958,5.514315962791443 l-0.7861190289258957,3.4051495790481567 m-13.767671585083008,-4.076680839061737 l1.9352631270885468,0.14704914763569832 12.778810262680054,-1.4860619604587555 l3.4806597232818604,-0.20606167614459991 1.695655882358551,0.11667067185044289 m-18.53834867477417,6.129122972488403 l1.7341388761997223,0.1762903854250908 8.258774280548096,-0.9937530755996704 l2.049257606267929,-0.06407605484127998 m-6.45605206489563,-10.413521528244019 l0.3124295361340046,0.5111020803451538 0.1576867699623108,0.8211614936590195 l-0.018231936264783144,14.58749532699585 -0.07374672219157219,0.781344398856163 l-0.37211861461400986,0.6553895771503448 -0.6588419526815414,0.2705141715705395 l-0.9687045216560364,-0.19850770011544228 m-1.4447002112865448,-6.051422953605652 l-0.9425057470798492,1.5914121270179749 -1.972879320383072,2.1568663418293 l-2.188502997159958,1.8543548882007599 -1.9710767269134521,1.228232979774475 m15.030159950256348,-7.4990785121917725 l1.5986396372318268,1.2455853819847107 1.8549199402332306,1.7624643445014954 l1.6361987590789795,1.8393374979496002 0.9100103378295898,1.3768230378627777 m-14.646083116531372,1.7961770296096802 l0.25494562461972237,0.6617558747529984 -0.02509830053895712,0.9623217582702637 l0.006324718706309795,11.023448705673218 m0.37413567304611206,-11.966822147369385 l10.044193267822266,-1.2438254058361053 0.5938883870840073,0.048141065053641796 l0.407155305147171,0.29235798865556717 0.24797983467578888,0.7846513390541077 l-0.14739621430635452,11.547727584838867 -0.1490433793514967,0.8272741734981537 l-0.25459861382842064,0.37565838545560837 -0.32467741519212723,0.017709024250507355 l-1.2107356637716293,-1.1722169816493988 -0.4732806980609894,-0.9400209784507751 m-7.981705069541931,-8.188024759292603 l8.677675127983093,-0.8334966003894806 m-8.532640933990479,4.6135783195495605 l8.431238532066345,-0.8023012429475784 m59.142762082628906,-18.020746560796397 l0.17777416855096817,0.15354246832430363 0.8223403990268707,0.06339532323181629 l8.420330882072449,-1.0547307133674622 0.9839878976345062,0.03396726446226239 m-5.186414122581482,-7.66020655632019 l0.5176249146461487,0.5808092653751373 0.19300591200590134,0.6721488386392593 l-0.11244932189583778,26.30802631378174 m0.0400740560144186,-18.605459928512573 l-1.2877637147903442,3.4140393137931824 -1.2064498662948608,2.6634815335273743 l-1.3769923150539398,2.5171712040901184 -1.9815617799758911,3.1718969345092773 m7.037372589111328,-9.222082495689392 l1.3088628649711609,1.3078837096691132 1.4568017423152924,2.034265846014023 m5.3422874212265015,-14.40599799156189 l0.20873120054602623,0.6390433758497238 0.024142146576195955,0.8633130043745041 l-1.6050492227077484,6.041586995124817 -1.0496801137924194,2.3292775452136993 l-1.207842230796814,1.8391899764537811 -1.4031900465488434,1.5850548446178436 l-1.3026930391788483,1.1653293669223785 m0.9363137185573578,-12.244415283203125 l0.6788013130426407,0.12970182113349438 0.7556915283203125,-0.012755307834595442 l8.899435997009277,-1.1623860895633698 1.354861557483673,-0.09573773480951786 l0.49289312213659286,0.14619012363255024 0.15393546782433987,0.2797950804233551 l0.014253414701670408,0.34187790006399155 -0.06072720047086477,0.38640785962343216 l-1.70674666762352,3.8166502118110657 -0.5516906082630157,1.2864373624324799 l0.09735357947647572,0.27215735986828804 0.3522762656211853,0.03695861203595996 l4.61295485496521,-0.6677523255348206 2.6258450746536255,-0.2847396954894066 l0.5597078427672386,0.14585389755666256 0.19428597763180733,0.5022977665066719 l-0.6692288815975189,2.356535494327545 -0.9865189343690872,2.7817052602767944 l-1.05465829372406,2.290487438440323 -1.0006438195705414,1.6199250519275665 l-0.7024767994880676,0.6282328069210052 -0.6014638021588326,0.07995042949914932 l-0.540136806666851,-0.3398372232913971 -0.8288031071424484,-0.9307469427585602 m-7.974919676780701,-5.977155566215515 l0.9710463136434555,0.045320503413677216 5.279421806335449,-0.9162227064371109 l0.4414772242307663,0.03295582486316562 0.2857402339577675,0.23542841896414757 l0.04185245372354984,0.3198586776852608 -1.7880968749523163,3.0467498302459717 l-1.562182456254959,1.8254932761192322 -1.8343773484230042,1.6266542673110962 m-0.5605995655059814,-4.068376123905182 l1.42866849899292,0.7986436039209366 1.493411660194397,1.1076033115386963 l1.2156681716442108,1.2366123497486115 0.8641603589057922,1.2182915955781937 m-9.152814745903015,3.810388445854187 l0.4987245425581932,1.005483865737915 0.832030326128006,6.1564236879348755 m-0.6722588837146759,-6.621975302696228 l13.102158308029175,-1.0524347424507141 0.7778926193714142,0.2394738420844078 l0.2865132689476013,0.5927302315831184 -1.052052453160286,5.80889105796814 m-9.187018275260925,-5.315847992897034 l0.239744670689106,0.7252862304449081 0.4330757260322571,5.138518810272217 m4.323222637176514,-6.217367053031921 l0.23918885737657547,0.5984202399849892 -0.36874227225780487,5.430350303649902 m-12.430624961853027,0.6870348751544952 l1.3720861077308655,0.1750851795077324 9.437054991722107,-0.6713346391916275 l5.96233069896698,-0.24565249681472778 3.407430350780487,-0.004130440065637231 l1.5605583786964417,0.18788130953907967 m53.79128840868361,-26.695093901726068 l0.16486085951328278,0.1848253794014454 0.3660373389720917,0.8015836775302887 l0.005518686957657337,11.817333698272705 -0.38813725113868713,4.688277542591095 l-0.4188309609889984,2.1443450450897217 -0.5822823941707611,1.9495312869548798 l-0.7991393655538559,1.9181615114212036 -1.0132494568824768,1.7899717390537262 m3.6417609453201294,-25.20432949066162 l4.596467018127441,-0.8715078979730606 0.7310714572668076,0.07980386726558208 l0.31555820256471634,0.37704750895500183 -0.2126377448439598,24.496314525604248 l-0.23095488548278809,0.7890328019857407 -0.31393110752105713,0.3209102153778076 l-0.3640395775437355,0.002023184351855889 -0.7893425226211548,-0.4960857331752777 m-3.427836000919342,-17.095109224319458 l4.425346553325653,-0.45561064034700394 m-4.557760953903198,7.084543704986572 l4.420696198940277,-0.42891599237918854 m8.287187814712524,-13.54259729385376 l0.10783965699374676,0.663517639040947 -0.24885902181267738,0.9144087880849838 l-1.1926312744617462,2.439761459827423 -1.53586745262146,2.42705836892128 l-1.793886125087738,2.1698959171772003 m4.51896995306015,-4.71881240606308 l0.936896800994873,0.1376811135560274 8.74513864517212,-1.353578120470047 l0.6612101197242737,0.06955613382160664 0.45831143856048584,0.3884395956993103 l0.152801051735878,0.7593144476413727 -0.6016216054558754,5.0897932052612305 l-0.6226868182420731,3.4636202454566956 -0.8662309497594833,3.3454862236976624 l-0.8808733522891998,2.575388252735138 -0.7984218001365662,1.5199458599090576 l-0.5078118667006493,0.30521210283041 -0.3132747858762741,0.006725693237967789 l-0.8928018063306808,-0.770956501364708 -0.5136393010616302,-0.8081060647964478 m-6.048606038093567,-9.147918224334717 l0.7947448641061783,0.17800530418753624 3.6059868335723877,-0.5435139685869217 l0.7582275569438934,0.07349140010774136 m-5.918382406234741,4.747588336467743 l0.6517398357391357,0.17259055748581886 5.09710431098938,-1.3021551072597504 l1.406317949295044,-0.03144932445138693 m-4.876378178596497,-2.512751817703247 l0.2751489542424679,0.667956992983818 0.026874507311731577,9.734262824058533 l-0.45332685112953186,0.7667499035596848 -0.4372498020529747,0.39453573524951935 l-0.38469281047582626,0.25312701240181923 -0.2613009139895439,0.14495085924863815 l-0.19258689135313034,0.05252507049590349 -0.1716368831694126,-0.015439516864717007 l-0.16891125589609146,-0.06879994180053473 m5.156771540641785,-5.3643423318862915 l0.8831226825714111,-0.1823159120976925 3.8609328866004944,-0.883733406662941 l0.7084377110004425,0.05229590926319361 0.48273392021656036,0.5612160637974739 l-0.338517501950264,1.6093473136425018 -0.7173603028059006,2.0955421030521393 l-0.968083068728447,2.056014835834503 -1.0455448925495148,1.62870392203331 l-1.085764765739441,1.2609970569610596 -1.2481382489204407,0.8537623286247253 l-1.2975817918777466,0.6143097579479218 m0.40216729044914246,-8.008045554161072 l0.9871029853820801,0.8585703372955322 1.1128849536180496,1.3518036901950836 l4.560354948043823,5.667327046394348 0.7647537440061569,0.6554336100816727 l0.7215210795402527,0.38239896297454834 \" fill=\"none\" stroke=\"black\" stroke-width=\"1\"/></svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "stroke_grid = make_grid_svg(reconstructions)\n",
    "draw_strokes(stroke_grid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def get_model_params():\n",
    "  # get trainable params.\n",
    "  model_names = []\n",
    "  model_params = []\n",
    "  model_shapes = []\n",
    "  with sess.as_default():\n",
    "    t_vars = tf.trainable_variables()\n",
    "    for var in t_vars:\n",
    "      param_name = var.name\n",
    "      p = sess.run(var)\n",
    "      model_names.append(param_name)\n",
    "      params = p\n",
    "      model_params.append(params)\n",
    "      model_shapes.append(p.shape)\n",
    "  return model_params, model_shapes, model_names\n",
    "\n",
    "def quantize_params(params, max_weight=10.0, factor=32767):\n",
    "  result = []\n",
    "  max_weight = np.abs(max_weight)\n",
    "  for p in params:\n",
    "    r = np.array(p)\n",
    "    r /= max_weight\n",
    "    r[r>1.0] = 1.0\n",
    "    r[r<-1.0] = -1.0\n",
    "    result.append(np.round(r*factor).flatten().astype(np.int).tolist())\n",
    "  return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model_params, model_shapes, model_names = get_model_params()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['vector_rnn/RNN/output_w:0',\n",
       " 'vector_rnn/RNN/output_b:0',\n",
       " 'vector_rnn/RNN/LSTMCell/W_xh:0',\n",
       " 'vector_rnn/RNN/LSTMCell/W_hh:0',\n",
       " 'vector_rnn/RNN/LSTMCell/bias:0']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model_names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# scale factor converts \"model-coordinates\" to \"pixel coordinates\" for your JS canvas demo later on.\n",
    "# the larger it is, the larger your drawings (in pixel space) will be.\n",
    "# I recommend setting this to 100.0 and iterating the value in the json file later on when you build the JS part.\n",
    "scale_factor = 200.0\n",
    "metainfo = {\"mode\":2,\"version\":6,\"max_seq_len\":train_set.max_seq_length,\"name\":\"custom\",\"scale_factor\":scale_factor}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model_params_quantized = quantize_params(model_params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model_blob = [metainfo, model_shapes, model_params_quantized]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open(\"custom.gen.full.json\", 'w') as outfile:\n",
    "  json.dump(model_blob, outfile, separators=(',', ':'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "After you dump the `custom.gen.full.json`, you should save the below code as `compress_model.json`, and run:\n",
    "\n",
    "```\n",
    "node compress_model.js custom.gen.full.json custom.gen.json\n",
    "```\n",
    "\n",
    "To get to the final file you can use for Sketch-RNN-JS\n",
    "\n",
    "Below is the entire code for `compress_model.js` which will be run using node:\n",
    "\n",
    "```\n",
    "/*\n",
    "compress_model.js\n",
    "Compress JSON model to b64 encoded version to save bandwidth. only works for decoder-only sketch-rnn model.\n",
    "*/\n",
    "\n",
    "const assert = require('assert');\n",
    "const fs = require('fs');\n",
    "\n",
    "/**\n",
    " * deals with decompressing b64 models to float arrays.\n",
    " */\n",
    "function btoa(s) {\n",
    "  return Buffer.from(s, 'binary').toString('base64');\n",
    "}\n",
    "function string_to_uint8array(b64encoded) {\n",
    "  var u8 = new Uint8Array(atob(b64encoded).split(\"\").map(function(c) {\n",
    "    return c.charCodeAt(0); }));\n",
    "  return u8;\n",
    "}\n",
    "function uintarray_to_string(u8) {\n",
    "  var s = \"\";\n",
    "  for (var i = 0, len = u8.length; i < len; i++) {\n",
    "    s += String.fromCharCode(u8[i]);\n",
    "  }\n",
    "  var b64encoded = btoa(s);\n",
    "  return b64encoded;\n",
    "};\n",
    "function string_to_array(s) {\n",
    "  var u = string_to_uint8array(s);\n",
    "  var result = new Int16Array(u.buffer);\n",
    "  return result;\n",
    "};\n",
    "function array_to_string(a) {\n",
    "  var u = new Uint8Array(a.buffer);\n",
    "  var result = uintarray_to_string(u);\n",
    "  return result;\n",
    "};\n",
    "\n",
    "var args = process.argv.slice(2);\n",
    "\n",
    "try {\n",
    "  assert.strictEqual(args.length, 2);\n",
    "} catch (err) {\n",
    "  console.log(\"Usage: node compress_model.js orig_full_model.json compressed_model.json\")\n",
    "  process.exit(1);\n",
    "}\n",
    "\n",
    "var orig_file = args[0];\n",
    "var target_file = args[1];\n",
    "\n",
    "var orig_model = JSON.parse(fs.readFileSync(orig_file, 'ascii'));\n",
    "\n",
    "var model_weights = orig_model[2];\n",
    "var compressed_weights = [];\n",
    "\n",
    "for (var i=0;i<model_weights.length;i++) {\n",
    "  compressed_weights.push(array_to_string(new Int16Array(model_weights[i])));\n",
    "}\n",
    "\n",
    "var target_model = [orig_model[0], orig_model[1], compressed_weights];\n",
    "\n",
    "fs.writeFileSync(target_file, JSON.stringify(target_model), 'ascii');\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
